﻿CREATE PROCEDURE [dbo].[yaf_nntptopic_savemessage](
                @NntpForumID INT,
                @Topic       NVARCHAR(100),
                @Body        NTEXT,
                @UserID      INT,
                @UserName    NVARCHAR(50),
                @IP          NVARCHAR(15),
                @Posted      DATETIME,
                @Thread      CHAR(32))
AS
    BEGIN
        DECLARE  @ForumID INT
        DECLARE  @TopicID INT
        DECLARE  @MessageID INT
        SELECT @ForumID = ForumID
        FROM   yaf_NntpForum
        WHERE  NntpForumID = @NntpForumID
        IF EXISTS (SELECT 1
                   FROM   yaf_NntpTopic
                   WHERE  Thread = @Thread)
        BEGIN
            -- thread exists
            SELECT @TopicID = TopicID
            FROM   yaf_NntpTopic
            WHERE  Thread = @Thread
        END
        ELSE
        BEGIN
            -- thread doesn't exists
            INSERT INTO yaf_Topic
                       (ForumID,
                        UserID,
                        UserName,
                        Posted,
                        Topic,
                        Views,
                        Priority,
                        NumPosts)
            VALUES     (@ForumID,
                        @UserID,
                        @UserName,
                        @Posted,
                        @Topic,
                        0,
                        0,
                        0)
            SET @TopicID = Scope_identity()
            INSERT INTO yaf_NntpTopic
                       (NntpForumID,
                        Thread,
                        TopicID)
            VALUES     (@NntpForumID,
                        @Thread,
                        @TopicID)
        END
        -- save message
        INSERT INTO yaf_Message
                   (TopicID,
                    UserID,
                    UserName,
                    Posted,
                    Message,
                    IP,
                    Position,
                    Indent)
        VALUES     (@TopicID,
                    @UserID,
                    @UserName,
                    @Posted,
                    @Body,
                    @IP,
                    0,
                    0)
        SET @MessageID = Scope_identity()
        -- update user
        IF EXISTS (SELECT 1
                   FROM   yaf_Forum
                   WHERE  ForumID = @ForumID
                   AND (Flags & 4) = 0)
        BEGIN
            UPDATE yaf_User
            SET    NumPosts = NumPosts + 1
            WHERE  UserID = @UserID
        END
        -- update topic
        UPDATE yaf_Topic
        SET    LastPosted = @Posted,
               LastMessageID = @MessageID,
               LastUserID = @UserID,
               LastUserName = @UserName
        WHERE  TopicID = @TopicID
        -- update forum
        UPDATE yaf_Forum
        SET    LastPosted = @Posted,
               LastTopicID = @TopicID,
               LastMessageID = @MessageID,
               LastUserID = @UserID,
               LastUserName = @UserName
        WHERE  ForumID = @ForumID
        AND (LastPosted IS NULL 
              OR LastPosted < @Posted)
    END


